//
//  UIButton+ADUtil.m
//  ADKit
//
//  Created by adu on 2018/6/11.
//

#import "UIButton+ADUtil.h"
#import "ADKitHeaders.h"

@implementation UIButton (ADLocalized)

- (void)setLocalized_title:(NSString *)localized_title {
    [self setTitle:localized_title.ad_localized forState:UIControlStateNormal];
}

- (void)setLocalized_title_selected:(NSString *)localized_title_selected {
    [self setTitle:localized_title_selected.ad_localized forState:UIControlStateSelected];
}

- (void)setLocalized_title_disable:(NSString *)localized_title_disable {
    [self setTitle:localized_title_disable.ad_localized forState:UIControlStateDisabled];
}

@end

@implementation UIButton (ADIBnspectable)

- (void)setNormalBackColor:(UIColor *)normalBackColor {
    [self setBackgroundImage:[UIImage imageWithColor:normalBackColor] forState:UIControlStateNormal];
}

- (void)setSelectBackColor:(UIColor *)selectBackColor {
    [self setBackgroundImage:[UIImage imageWithColor:selectBackColor] forState:UIControlStateSelected];
}

- (void)setHighlightBackColor:(UIColor *)highlightBackColor {
    [self setBackgroundImage:[UIImage imageWithColor:highlightBackColor] forState:UIControlStateHighlighted];
}

- (void)setDisableBackColor:(UIColor *)disableBackColor {
    [self setBackgroundImage:[UIImage imageWithColor:disableBackColor] forState:UIControlStateDisabled];
}

- (void)setNormalHexColor:(NSString *)normalHexColor {
    [self setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:normalHexColor]] forState:UIControlStateNormal];
}

- (void)setSelectHexColor:(NSString *)selectHexColor {
    [self setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:selectHexColor]] forState:UIControlStateSelected];
}

- (void)setHighlightHexColor:(NSString *)highlightHexColor {
    [self setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:highlightHexColor]] forState:UIControlStateHighlighted];
}

- (void)setDisableHexColor:(NSString *)disableHexColor {
    [self setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:disableHexColor]] forState:UIControlStateDisabled];
}

@end

@implementation UIButton (ADUtil)

- (void)setLayout:(ADButtonLayout)layout spacing:(CGFloat)spacing {    
    CGFloat imageWidth = self.imageView.image.size.width;
    CGFloat imageHeight = self.imageView.image.size.height;
    
    CGSize labelSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName : self.titleLabel.font}];
    CGFloat labelWidth = labelSize.width;
    CGFloat labelHeight = labelSize.height;
    
    CGFloat imageOffsetX = (imageWidth + labelWidth) / 2 - imageWidth / 2;//image中心移动的x距离
    CGFloat imageOffsetY = imageHeight / 2 + spacing / 2;//image中心移动的y距离
    CGFloat labelOffsetX = (imageWidth + labelWidth / 2) - (imageWidth + labelWidth) / 2;//label中心移动的x距离
    CGFloat labelOffsetY = labelHeight / 2 + spacing / 2;//label中心移动的y距离
    
    CGFloat tempWidth = MAX(labelWidth, imageWidth);
    CGFloat changedWidth = labelWidth + imageWidth - tempWidth;
    CGFloat tempHeight = MAX(labelHeight, imageHeight);
    CGFloat changedHeight = labelHeight + imageHeight + spacing - tempHeight;
    
    switch (layout) {
        case ADButtonLayoutNormal:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
            
        case ADButtonLayoutImageRight:
            self.imageEdgeInsets = UIEdgeInsetsMake(0, labelWidth + spacing/2, 0, -(labelWidth + spacing/2));
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -(imageWidth + spacing/2), 0, imageWidth + spacing/2);
            self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
            break;
            
        case ADButtonLayoutImageTop:
            self.imageEdgeInsets = UIEdgeInsetsMake(-imageOffsetY, imageOffsetX, imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(labelOffsetY, -labelOffsetX, -labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(imageOffsetY, -changedWidth/2, changedHeight-imageOffsetY, -changedWidth/2);
            break;
            
        case ADButtonLayoutImageBottom:
            self.imageEdgeInsets = UIEdgeInsetsMake(imageOffsetY, imageOffsetX, -imageOffsetY, -imageOffsetX);
            self.titleEdgeInsets = UIEdgeInsetsMake(-labelOffsetY, -labelOffsetX, labelOffsetY, labelOffsetX);
            self.contentEdgeInsets = UIEdgeInsetsMake(changedHeight-imageOffsetY, -changedWidth/2, imageOffsetY, -changedWidth/2);
            break;
    }
}

@end
